# encoding:utf8
import json

import pymongo
import scrapy

from ..settings import *


class WulianYunCang(scrapy.Spider):
    name = 'wlyc'

    custom_settings = {
        'DEFAULT_REQUEST_HEADERS': {
            'Host': "m.50yc.com",
            'Accept': "application/json, text/javascript, */*; q=0.01",
            'X-Requested-With': "XMLHttpRequest",
            'Accept-Language': "zh-cn",
            'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8",
            'Origin': "https://m.50yc.com",
            'User-Agent': "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 MicroMessenger/7.0.3(0x17000321) NetType/WIFI Language/zh_CN",
            'Cache-Control': "no-cache",
            'Accept-Encoding': "gzip, deflate",
        },
        'IMAGES_STORE': '/data/images/wulianyuncang/',
        'ITEM_PIPELINES': {
            'baiguoyuan.pipelines.GoodsImagesPipeline': 1,
            'baiguoyuan.pipelines.CommonPipelinesDropDuplicated': 320
        },
    }

    def __init__(self):
        self.client = pymongo.MongoClient(host=MONGO_URI)
        self.db = self.client[MONGO_DATABASE]
        self.collection = 'city_district_all'
        super(WulianYunCang, self).__init__()

    def start_requests(self):
        url = 'https://m.50yc.com/wapi/map/Relative'
        payload = "pos={lng}%2C{lat}&l=5000000&p=1&o=7"
        locations = self.db.get_collection(self.collection).find({'tp': 'ct'}, {'name': 1, 'center': 1})
        for loc in locations:
            lng, lat = loc.get('center').split(',')
            pl = payload.format(lng=lng, lat=lat)
            yield scrapy.Request(url, method='POST', callback=self.parse_detail, body=pl,
                                 meta={'lng': lng, 'lat': 'lat', 'page': 1}, dont_filter=True)

    def parse_detail(self, response):
        data = response.text
        data = json.loads(data)
        data.update(
            {'data_collection': 'wulianyuncang', 'data_from': 'List', 'drop_by': 'RptNumber'})
        image_urls = [("https://static.50yc.com/" + item.get('ImageSrc'), 'src') for item in data.get('List')]
        image_header = {
            'Host': "static.50yc.com",
            'User-Agent': "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 MicroMessenger/7.0.3(0x17000321) NetType/WIFI Language/zh_CN",
        }
        data.update({'image_urls': image_urls, 'image_header': image_header})
        yield data
        if not data.get('LastPage'):
            url = 'https://m.50yc.com/wapi/map/Relative'
            payload = "pos={lng}%2C{lat}&l=5000000&p={page}&o=7"
            lng = response.meta.get('lng')
            lat = response.meta.get('lat')
            page = response.meta.get('page')
            pg = page + 1
            pl = payload.format(lng=lng, lat=lat, page=pg)
            yield scrapy.Request(url, method='POST', callback=self.parse_detail, body=pl, meta={'page': pg},
                                 dont_filter=True)
